/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: app2022 - BarCode1
 * <p>Powered by webrx On 2022-01-24 11:19:42
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class BarCode1 {
    public static void main(String[] args) throws WriterException, IOException {
        //生成条码
        Map hints = new HashMap<EncodeHintType, Object>();
        var b = new MultiFormatWriter().encode("6901285991271", BarcodeFormat.EAN_13, 300, 220, hints);
        var i = MatrixToImageWriter.toBufferedImage(b);
        ImageIO.write(i, "png", new File("bar.png"));

        //解析条码
        LuminanceSource source = new BufferedImageLuminanceSource(ImageIO.read(new File("qq.jpg")));
        BinaryBitmap image = new BinaryBitmap(new HybridBinarizer(source));
        try {
            Result result = new MultiFormatReader().decode(image);
            String info = result.getText();
            System.out.println(info);
            if (info == null || "".equals(info)) {
            } else {
                //输出条码  88886666
                System.out.println(info);
            }
        } catch (Exception e) {
            System.out.println("图像中没有二维码");
        }
    }
}
